<template>
  <div class="video-grid-container">
    <!-- 分类标签 -->
    <div class="category-tabs-container">
      <div class="category-tabs" ref="categoryTabsRef">
        <div 
          v-for="category in categories" 
          :key="category.id"
          :class="['tab-item', { active: activeCategory === category.id }]"
          @click="selectCategory(category.id)"
        >
          {{ category.name }}
        </div>
      </div>
      <div class="category-nav-buttons">
        <button 
          class="nav-btn prev-btn" 
          @click="scrollCategories('left')"
          :disabled="scrollLeft === 0"
        >
          <LeftOutlined />
        </button>
        <button 
          class="nav-btn next-btn" 
          @click="scrollCategories('right')"
          :disabled="scrollRight === 0"
        >
          <RightOutlined />
        </button>
      </div>
    </div>

    <!-- 视频内容网格 -->
    <div class="video-grid">
      <div v-for="video in videos" :key="video.id" class="video-card" @click="playVideo(video)">
        <div class="video-cover">
          <img :src="video.cover" :alt="video.title" />
          <div class="video-overlay">
            <PlayCircleOutlined class="play-icon" />
          </div>
          <div class="video-duration">{{ video.duration }}</div>
        </div>
        
        <div class="video-content">
          <h3 class="video-title">{{ video.title }}</h3>
          <div class="video-meta">
            <div class="creator-info">
              <a-avatar :src="video.creator.avatar" :size="24" />
              <span class="creator-name">{{ video.creator.name }}</span>
            </div>
            <div class="video-stats">
              <span class="stat-item">
                <HeartOutlined />
                {{ formatNumber(video.likes) }}
              </span>
              <span class="stat-item">
                <MessageOutlined />
                {{ formatNumber(video.comments) }}
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, nextTick } from 'vue'
import { message } from 'ant-design-vue'
import {
  PlayCircleOutlined,
  HeartOutlined,
  MessageOutlined,
  LeftOutlined,
  RightOutlined
} from '@ant-design/icons-vue'

// Props
interface Video {
  id: number
  title: string
  cover: string
  duration: string
  creator: {
    name: string
    avatar: string
  }
  likes: number
  comments: number
}

interface Category {
  id: string
  name: string
}

interface Props {
  videos?: Video[]
  categories?: Category[]
}

const props = withDefaults(defineProps<Props>(), {
  videos: () => [],
  categories: () => []
})

// Emits
const emit = defineEmits<{
  categoryChange: [categoryId: string]
  videoPlay: [video: Video]
}>()

// 当前选中的分类
const activeCategory = ref('all')

// 分类滚动相关
const categoryTabsRef = ref<HTMLElement>()
const scrollLeft = ref(0)
const scrollRight = ref(1)

// 方法
const selectCategory = (categoryId: string) => {
  activeCategory.value = categoryId
  emit('categoryChange', categoryId)
}

const scrollCategories = (direction: 'left' | 'right') => {
  if (!categoryTabsRef.value) return
  
  const container = categoryTabsRef.value
  const scrollAmount = 200
  
  if (direction === 'left') {
    container.scrollLeft -= scrollAmount
  } else {
    container.scrollLeft += scrollAmount
  }
  
  updateScrollButtons()
}

const updateScrollButtons = () => {
  if (!categoryTabsRef.value) return
  
  const container = categoryTabsRef.value
  scrollLeft.value = container.scrollLeft
  scrollRight.value = container.scrollWidth - container.clientWidth - container.scrollLeft
}

const playVideo = (video: Video) => {
  emit('videoPlay', video)
  message.success('正在播放视频...')
}

const formatNumber = (num: number) => {
  if (num >= 10000) {
    return (num / 10000).toFixed(1) + '万'
  }
  return num.toString()
}

onMounted(() => {
  nextTick(() => {
    updateScrollButtons()
    if (categoryTabsRef.value) {
      categoryTabsRef.value.addEventListener('scroll', updateScrollButtons)
    }
  })
})
</script>

<style scoped>
.video-grid-container {
  width: 100%;
}

.category-tabs-container {
  position: relative;
  margin-bottom: 24px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.category-tabs {
  display: flex;
  gap: 16px;
  overflow-x: hidden;
  padding-bottom: 8px;
  flex: 1;
  scroll-behavior: smooth;
}

.category-tabs::-webkit-scrollbar {
  display: none;
}

.category-tabs {
  -ms-overflow-style: none;
  scrollbar-width: none;
}

.tab-item {
  padding: 8px 16px;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s;
  color: #ddd;
  white-space: nowrap;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.tab-item:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.tab-item.active {
  background: #ff4757;
  color: #fff;
  border-color: #ff4757;
}

.category-nav-buttons {
  display: flex;
  gap: 8px;
}

.nav-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s;
  flex-shrink: 0;
}

.nav-btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.3);
}

.nav-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.nav-btn svg {
  font-size: 14px;
}

.video-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 24px;
  width: 100%;
}

.video-card {
  background: rgba(15, 52, 96, 0.8);
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.video-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  border-color: rgba(255, 255, 255, 0.2);
}

.video-cover {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.video-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.video-card:hover .video-cover img {
  transform: scale(1.05);
}

.video-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s;
}

.video-card:hover .video-overlay {
  opacity: 1;
}

.play-icon {
  font-size: 48px;
  color: #fff;
}

.video-duration {
  position: absolute;
  bottom: 8px;
  right: 8px;
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12px;
}

.video-content {
  padding: 16px;
}

.video-title {
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  margin-bottom: 12px;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.video-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.creator-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.creator-name {
  font-size: 14px;
  color: #999;
}

.video-stats {
  display: flex;
  gap: 12px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #999;
}

@media (max-width: 768px) {
  .video-grid {
    grid-template-columns: 1fr;
  }
}
</style>
